/*----------------------------CLASS ERROR-----------------------------*/
Error = function (data) {
   this._FORM_ERROR_CLASS = 'error';

   this.id = data.id;
   this._controlId = data.controlId || data.id;
   this.name = data.name || '';
   this.message = data.message || '';
   this.show = data.show || false;

   this._decorateHandler = (typeof(data.decorateHandler) == "undefined") ? this._defaultDecorateHandler : data.decorateHandler;
   this._unDecorateHandler = (typeof(data.unDecorateHandler) == "undefined") ? this._defaultUnDecorateHandler : data.unDecorateHandler;

   this._formErrorClass = this._FORM_ERROR_CLASS;
};

Error.prototype.showError = function () {
   if (this._decorateHandler) {
      this._decorateHandler();
   }
};

Error.prototype.hideError = function () {
   if (this._unDecorateHandler) {
      this._unDecorateHandler();
   }
};

Error.prototype._defaultDecorateHandler = function () {
   var item, itemTooltip;
   if (typeof(this._controlId) == "array") {
      for (var i = 0; i < this._controlId.length; i++) {
         item = $('#' + this._controlId[i]);
         if (item) {
            item.addClass(this._formErrorClass);

            // set error message
            item.attr('title', this.message);
         }
         else {
            console.log('Element ' + this._controlId + ' does not exist.');
         }
      }
   }
   else if (typeof(this._controlId) == "string") {
      item = $('#' + this._controlId);
      if (item) {
         item.addClass(this._formErrorClass);

         // set error message
         item.attr('title', this.message);
      }
      else {
         console.log('Element ' + this._controlId + ' does not exist.');
      }
   }
};

Error.prototype._defaultUnDecorateHandler = function () {
   var item, itemTooltip;

   if (typeof(this._controlId) == "array") {
      for (var i = 0; i < this._controlId.length; i++) {
         item = $('#' + this._controlId[i]);
         if (item) {
            item.removeClass(this._formErrorClass);
         }
         else {
            console.log('Element ' + this._controlId + ' does not exist.');
         }
      }
   }
   else if (typeof(this._controlId) == "string") {
      item = $('#' + this._controlId);
      if (item) {
         item.removeClass(this._formErrorClass);
      }
      else {
         console.log('Element ' + this._controlId + ' does not exist.');
      }
   }
};
/*----------------------------CLASS ERROR LIST-----------------------------*/
ErrorList = function () {
   this._errorMap = {};
};

ErrorList.prototype.reset = function () {
   this._errorMap = {};
};

ErrorList.prototype.addError = function (errorObj) {
   var error = new Error(errorObj);
   this._errorMap[error.id] = error;
   if (error.show) {
      error.showError();
   }
};

ErrorList.prototype.removeError = function (errorId) {
   var error = this._errorMap[errorId];
   if (error) {
      error.hideError();

      // remove this error
      // Note: delete errorObj will not work
      delete this._errorMap[errorId];
   }
};

ErrorList.prototype.hasError = function () {
   for (var errorId in this._errorMap) {
      if (this._errorMap.hasOwnProperty(errorId)) {
         return true;
      }
   }

   return false;
};

ErrorList.prototype.containError = function (errorId) {
   var containError = false;
   if (this._errorMap[errorId]) {
      containError = true;
   }

   return containError;
};

ErrorList.prototype.report = function () {
   var errorDetails = '';
   for (var errorId in this._errorMap) {
      if (this._errorMap.hasOwnProperty(errorId)) {
         var error = this._errorMap[errorId];
         errorDetails += error.name + ": " + error.message + "<br>";

         // show error if not shown before
         if (!error.show) {
            error.show = true;
            error.showError();
         }
      }
   }

   ISS.DOM.html('#iss_dialog_error', errorDetails);
   ISS.CONTROL.dialog('#iss_dialog_error', {
      height: 150,
      width: 280,
      modal: true
   });
};

ErrorList.prototype.clearErrors = function () {
   for (var errorId in this._errorMap) {
      if (this._errorMap.hasOwnProperty(errorId)) {
         this.removeError(errorId);
      }
   }
};

ErrorList.prototype.each = function (callback) {
   for (var errorId in this._errorMap) {
      if (this._errorMap.hasOwnProperty(errorId)) {
         callback(this._errorMap[errorId]);
      }
   }
};

/**/
Validation = {
   ERROR_CODE_OK: 0,
   ERROR_CODE_FAIL: -1,

   validatePattern: function (str, pattern) {
      var result = false;
      if (str && (str.search(pattern) > -1)) {
         result = true;
      }

      return result;
   },

   validateEmail: function (email, optional) {
      var result = {
         code: this.ERROR_CODE_FAIL
      };

      if (email === '' && optional) {
         result.code = this.ERROR_CODE_OK;
      }
      else if (email === '' && !optional) {
         result.code = this.ERROR_CODE_FAIL;
      }
      else {
         var reg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
         if (this.validatePattern(email, reg)) {
            result.code = this.ERROR_CODE_OK;
         }
      }

      return result;
   },

   formatPhoneNumber: function (phoneNumber, optional) {
      var result = {
         code: this.ERROR_CODE_FAIL,
         formattedNumber: phoneNumber
      };

      var _optional = optional || false;
      if (!phoneNumber) {
         if (_optional) {
            result.code = this.ERROR_CODE_OK;
         }
         else {
            result.message = ISS_STRING.MESSAGE_FIELD_CANNOT_EMPTY;
         }
      }
      // check format and assign new auto-formatted value
      else if (this.validatePattern(phoneNumber, /^\d{3}-\d{3}-\d{4}$/)) {
         result.code = this.ERROR_CODE_OK;
         result.formattedNumber = phoneNumber;
      }
      else if (this.validatePattern(phoneNumber, /^\d{10}$/)) {
         result.code = this.ERROR_CODE_OK;
         result.formattedNumber = phoneNumber.substr(0, 3) + '-' + phoneNumber.substr(3, 3) + '-' + phoneNumber.substr(6);
      }
      else if (this.validatePattern(phoneNumber, /^\d{3}-\d{7}$/)) {
         result.code = this.ERROR_CODE_OK;
         result.formattedNumber = phoneNumber.substr(0, 7) + '-' + phoneNumber.substr(7);
      }
      else if (this.validatePattern(phoneNumber, /^\d{6}-\d{4}$/)) {
         result.code = this.ERROR_CODE_OK;
         result.formattedNumber = phoneNumber.substr(0, 3) + '-' + phoneNumber.substr(3);
      }

      return result;
   }
};